package com.infore.siteManagement.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.infore.siteManagement.constant.ResultConstants;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.util.ArrayUtils;
import com.infore.siteManagement.util.DateUtil;
import com.infore.siteManagement.util.FileUtil;
import com.infore.siteManagement.util.UUIDGenerator;
import com.infore.siteManagement.entity.SiteBasicFile;
import com.infore.siteManagement.repository.ISiteBasicFileRepository;
import com.infore.siteManagement.service.IFileService;
import com.infore.siteManagement.util.ResultEntity;

@Service
public class FileServiceImpl implements IFileService {
	@Autowired
	private ISiteBasicFileRepository iSiteBasicFileRepository;

	public ResultEntity dowloadFile(HttpServletResponse response, String fileId) {
		try {
			SiteBasicFile siteBasicFile = iSiteBasicFileRepository.findById(fileId).orElse(null);
			if (siteBasicFile == null) {
				return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.NO_SUCH_OBJECT);
			}
			String name = siteBasicFile.getFileName();
			String filePath = siteBasicFile.getFilePath();
			String suffix = FileUtil.fileNameSuffix(filePath);
			name = name + suffix;
			new FileUtil().downloadFileRes(response, filePath, name);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ResultEntity delete(String fileId) {
		List<String> ids = ArrayUtils.stringToList(fileId);
		List<SiteBasicFile> siteBasicFiles = iSiteBasicFileRepository.findByIdIn(ids);
		for (SiteBasicFile siteBasicFile : siteBasicFiles) {
			try {
				String path = siteBasicFile.getFilePath();
				File file = new File(FileUtil.BASE_PATH + path);
				if (file.delete()) {
					iSiteBasicFileRepository.delete(siteBasicFile);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return new ResultEntity(ResultStatus.SUCCESS, ResultConstants.DELETE_FILE);
	}

	public ResultEntity uploadSingle(String path, MultipartFile[] multipartFiles) {
		List<SiteBasicFile> list = new ArrayList<SiteBasicFile>();
		if (multipartFiles == null || multipartFiles.length == 0) {
			return null;
		}
		String partPath = FileUtil.BASE_PATH + path;// 目录路径
		File fileMkdir = new File(partPath);// 判断文件目录是否存在
		if (!fileMkdir.exists()) {
			fileMkdir.mkdirs();
		}
		for (MultipartFile multipartFile : multipartFiles) {
			try {
				SiteBasicFile siteBasicFile = new SiteBasicFile();
				// 取得当前上传文件的文件名称
				String originalFilename = multipartFile.getOriginalFilename();
				// 得到文件后缀
				String[] fileName = originalFilename.split("\\.");
				// 生成文件名
				String uuid = UUIDGenerator.getUUID();
				String fileType = FileUtil.fileType(originalFilename);
				String name = uuid + "." + fileName[fileName.length - 1];
				siteBasicFile.setId(uuid);
				siteBasicFile.setFileName(fileName[0]);
				siteBasicFile.setFilePath(path + name);
				siteBasicFile.setFilleType(fileType);
				siteBasicFile.setUploadTime(DateUtil.getSystemTime());
				File file = new File(partPath, name);
				multipartFile.transferTo(file);
				list.add(siteBasicFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		if (list == null || list.size() <= 0) {
			return null;
		}
		return new ResultEntity().setDataList(list);
	}
}
